#include "widget.h"
#include "ui_widget.h"
#include  <QPainter>
#include <QVector>
#include <QMouseEvent>
#include <math.h>


Widget * me;
float alpha=0,beta=0;
union Vector {
    struct {float x,y,z;};
    struct {float r,g,b;};
    Vector(){x=y=z=0;}
    float v[3];
    Vector(float xx, float yy, float zz){
        x=xx;y=yy;z=zz;
    }
    Vector operator+(Vector v){
        return Vector(x+v.x, y+v.y, z+v.z);
    }
    Vector operator-(Vector v){
        return Vector(x-v.x, y-v.y, z-v.z);
    }
    Vector mul(Vector v){
        return Vector(x*v.x, y*v.y, z*v.z);
    }
};





QVector<Vector> makeUglySphere(){
    float a,b;
    QVector<Vector> result;
    float pi=atan(1)*4;
    float step=0.4;
    for(a=0;a<pi*2;a+=step){
        for(b=-pi/2;b<pi/2+0.05;b+=step){
            float aa,bb;
            result.push_back(Vector(
                    sin(a)*cos(b),
                    cos(a)*cos(b),
                    sin(b)));
            a+=step;
            result.push_back(Vector(
                    sin(a)*cos(b),
                    cos(a)*cos(b),
                    sin(b)));
            result.push_back(Vector(
                    sin(a)*cos(b),
                    cos(a)*cos(b),
                    sin(b)));
            b+=step;
            result.push_back(Vector(
                    sin(a)*cos(b),
                    cos(a)*cos(b),
                    sin(b)));
            result.push_back(Vector(
                    sin(a)*cos(b),
                    cos(a)*cos(b),
                    sin(b)));
            b-=step;
            result.push_back(Vector(
                    sin(a)*cos(b),
                    cos(a)*cos(b),
                    sin(b)));
            result.push_back(Vector(
                    sin(a)*cos(b),
                    cos(a)*cos(b),
                    sin(b)));
            a-=step;
            result.push_back(Vector(
                    sin(a)*cos(b),
                    cos(a)*cos(b),
                    sin(b)));
        }
    }
    return result;
}

Widget::Widget(QWidget *parent)
    : QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);
    me=this;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    this->close();
}


Vector transform(Vector v) {

    v=Vector( v.x*cos(alpha)-v.y*sin(alpha),
              v.x*sin(alpha)+v.y*cos(alpha),
              v.z);

    v=Vector( v.x,
              v.y*cos(beta)-v.z*sin(beta),
              v.y*sin(beta)+v.z*cos(beta)
              );

    v=Vector(v.x, -v.z, v.y);
    v.z-=4;
    v.x/=v.z;
    v.y/=v.z;
    v=v.mul(Vector(200.0,200.0,1.0));
    v=v+Vector(me->width()/2, me->height()/2, 0);
    //v=v.mul(Vector(me->height()*0.4, me->height()*0.4, 1));
    return v ;
}

void Widget::paintEvent(QPaintEvent *){
    QPainter p(this);
    int a;
    QVector<Vector> sphere= makeUglySphere();
    for(a=0;a<sphere.size()-1;a+=2){
        Vector v1,v2;
        v1=transform(sphere[a+0]);
        v2=transform(sphere[a+1]);

        p.drawLine(v1.x,v1.y,v2.x,v2.y);
    }
}

void Widget::mousePressEvent(QMouseEvent *){

}
void Widget::mouseReleaseEvent(QMouseEvent *){

}
void Widget::mouseMoveEvent(QMouseEvent *e){
    alpha = e->x()*0.01;
    beta  = e->y()*0.01;

    this->repaint();
}

